function unExpect() {
    let count = 10000;
    return () => {
        count--;
        if(count < 0) {
            throw new Error('unexpect!!!');
        }
    }
}
export namespace Formula {
    let operates = ['+', '-', '*', '/',':', ','];
    let funcs: any[] = ['SUM','IF'];
    const positive = 'POSITIVE';
    const negative = 'NEGATIVE';
    let keywords = ['TRUE', 'FALSE'];
    let catchErr = unExpect();
    export function parse(formulaStr: string) {
        let unexpectCount = 10000;
        formulaStr = formulaStr.substring(1);
        let result: {type: 'func'|'string'|'number'|'operate'|'cell'|'brackets'|'keyword'|'symbol',value: string}[] = [];
        catchErr();
        while(formulaStr.length > 0) {
            catchErr();
            unexpectCount--;
            if(unexpectCount < 0) {
                console.error('=========');
                break;
            }
            try {
                for(let i = 0; i < formulaStr.length; i++) {
                    catchErr();
                    if(['(', ')'].indexOf(formulaStr[i]) !== -1) {
                        result.push({type: 'brackets', value: formulaStr[0]});
                        formulaStr = formulaStr.substring(1);
                        break;
                    }
                    if(operates.indexOf(formulaStr[i]) !== -1) {
                        catchErr();
                        if(formulaStr[0] === '+' || formulaStr[0] === '-') {
                            if(result.length === 0 || result[result.length - 1].type === 'operate') {
                                result.push({type: 'symbol', value: formulaStr[0]});
                            } else {
                                result.push({type: 'operate', value: formulaStr[0]});
                            }
                        }else{
                            result.push({type: 'operate', value: formulaStr[0]});
                        }
                        formulaStr = formulaStr.substring(1);
                        throw '';
                    } 
                    // 处理字符串
                    if(formulaStr[i] === '"') {
                        for(let j = i+1; j < formulaStr.length; j++) {
                            if(formulaStr[j] === '\\') {
                                j++;
                                continue;
                            }
                            if(formulaStr[j] === '"') {
                                let temp = formulaStr.substring(i+1, j);
                                formulaStr = formulaStr.substring(j+1);
                                result.push({type: 'string', value: temp});
                                throw 'first';
                            }
                        }
                    }
                    if(/[A-Z]/.test(formulaStr[0]) === true) {
                        let isBreak = false;
                        for(let j = i+1; j < formulaStr.length; j++) {
                            if([...operates, '(',')', ...funcs].indexOf(formulaStr[j]) !== -1) {
                                let temp = formulaStr.substring(i, j);
                                formulaStr = formulaStr.substring(j);
                                if(temp.length === 1||/^[A-Z]+[0-9]+$/.test(temp)) {
                                    result.push({type: 'cell', value: temp});
                                    isBreak = true;
                                    throw temp
                                }
                                // 处理关键字 
                                if(keywords.indexOf(temp) !== -1) {
                                    result.push({type: 'keyword', value: temp});
                                    isBreak = true;
                                    break;
                                }
                                if(funcs.indexOf(temp) !== -1) {
                                    result.push({type: 'func', value: temp});
                                    isBreak = true;
                                    break;
                                }
                                catchErr();
                                break;
                            }
                        }
                        if(!isBreak) {
                            if(/^[A-Z]+[0-9]+$/.test(formulaStr)) {
                                result.push({type: 'cell', value: formulaStr});
                                formulaStr = '';
                            }
                            if(keywords.indexOf(formulaStr) !== -1) {
                                result.push({type: 'keyword', value: formulaStr});
                                formulaStr = '';
                            }
                            break;
                        }
           
                    }
                    if(result[result.length - 1].value === 'A2') {
                 
                    }
                    // console.log(formulaStr)
                }
            } catch (error) {

            }
        }
    }
}